<template>
  <div>
    <el-form
      ref="deductionsForm"
      :rules="rules"
      :model="deductionsBase"
      label-position="right"
      label-width="80px"
      class="titmInfo"
    >
      <el-form-item label="部门：" prop="departmentId">
        <el-select
          v-model="deductionsBase.departmentId"
          placeholder="请选择"
          @change="handleChangeDeductions"
        >
          <el-option
            v-for="item in departmentData"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <el-table ref="singleTable" :data="stateData.departmentType" style="width: 100%">
      <el-table-column>
        <template slot-scope="scope">
          <div>
            {{ scope.row.name }}
            <el-switch
              v-model="scope.row.isEnable"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </div>

          <div v-if="scope.row.dedTypeCode==='51000'" class="attentInfo">
            <p>
              迟到≤
              <el-input
                v-model="scope.row.periodUpperLimit"
                class="inputInfo"
                value="30"
              />分钟
            </p>
            <div class="deductionInfo">
              <p>
                迟到≤
                <el-input
                  v-model="scope.row.timesUpperLimit"
                  class="inputInfo"
                />次，每次扣款
                <el-input
                  v-model="scope.row.dedAmonutUpperLimit"
                  class="inputInfo"
                />元
              </p>
              <p>
                迟到>
                <el-input
                  v-model="scope.row.timesLowerLimit"
                  class="inputInfo"
                  disabled
                />次，每次扣款
                <el-input
                  v-model="scope.row.dedAmonutLowerLimit"
                  class="inputInfo"
                />元
              </p>
            </div>
            <p>
              迟到>
              <el-input v-model="scope.row.periodLowerLimit" class="inputInfo" disabled />分钟
            </p>
            <div class="deductionInfo">
              <p>
                迟到>
                <el-input
                  v-model="scope.row.absenceTimesUpperLimt"
                  class="inputInfo"
                  disabled
                />次，每次矿工
                <el-input
                  v-model="scope.row.absenceDays"
                  class="inputInfo"
                />天
              </p>
            </div>
          </div>
          <div v-if="scope.row.dedTypeCode==='52000'" class="attentInfo">
            <p>
              早退≤
              <el-input
                v-model="scope.row.periodUpperLimit"
                class="inputInfo"
                value="30"
              />分钟
            </p>
            <div class="deductionInfo">
              <p>
                早退≤
                <el-input
                  v-model="scope.row.timesUpperLimit"
                  class="inputInfo"
                />次，每次扣款
                <el-input
                  v-model="scope.row.dedAmonutUpperLimit"
                  class="inputInfo"
                />元
              </p>
              <p>
                早退>
                <el-input v-model="scope.row.timesLowerLimit" class="inputInfo" disabled />次，每次扣款
                <el-input
                  v-model="scope.row.dedAmonutLowerLimit"
                  class="inputInfo"
                />元
              </p>
            </div>
            <p>
              早退>
              <el-input v-model="scope.row.periodLowerLimit" class="inputInfo" disabled />分钟
            </p>
            <div style="padding-left:120px;">
              <p>
                早退>
                <el-input
                  v-model="scope.row.absenceTimesUpperLimt"
                  class="inputInfo"
                  disabled
                />次，每次矿工
                <el-input
                  v-model="scope.row.absenceDays"
                  class="inputInfo"
                />天
              </p>
            </div>
          </div>
          <div v-if="scope.row.dedTypeCode==='53000'" class="attentInfo">
            <p>
              矿工按
              <el-input
                v-model="scope.row.fineSalaryMultiples"
                class="inputInfo"
              />倍工资处罚
            </p>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="el-dialog__footer dialog-footer">
      <el-button type="primary" @click="btnOK">保存更新</el-button>
      <el-button @click="btnCanel">取消</el-button>
    </div>
  </div>
</template>

<script>
import attendanceApi from '@/api/constant/attendance'
import { getDepartments } from '@/api/departments'
import { payOffByDepartId, deductionsSave } from '@/api/attendances'
export default {
  props: {
    settingDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      deductionsBase: {
        departmentId: ''
      },
      departmentData: [],
      stateData: [],
      rules: {
        // 表单验证
        // name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        departmentId: [
          { required: true, message: '请选择部门', trigger: 'change' }
        ]

      }

    }
  },
  created() {
    this.getDepartments()
    this.stateData = attendanceApi
  },
  methods: {
    // 获取部门
    async getDepartments() {
      const { depts } = await getDepartments()
      this.departmentData = depts
    },
    async handleChangeovertime(val) {
      this.deductionsBase.departmentId = val
      this.stateData.departmentType.forEach(item => {
        item.departmentId = val
        item.isEnable = false
      })
      const fd = new FormData()
      fd.append('departmentId', val)
      const res = await payOffByDepartId(fd)
      res.forEach(item => {
        if (item.isEnable === 0) {
          item.isEnable = true
        } else {
          item.isEnable = false
        }
        // console.log(item)
        this.stateData.departmentType.forEach(val => {
          if (val.dedTypeCode === item.dedTypeCode) {
            val.isEnable = item.isEnable
          }
        })
      })
      // console.log(res)
    },
    async btnOk() {
      this.$refs.deductionsForm.validate(async res => {
        if (res) {
          const data = this.stateData.departmentType
          data.forEach(item => {
            if (item.isEnable) {
              item.isEnable = '0'
            } else {
              item.isEnable = '1'
            }
          })
          await deductionsSave(data)
          this.$message.success('操作成功')
          await this.btnCanel()
        }
      })
    },
    btnCanel() {
      this.$refs.deductionsForm.resetFields()
      this.$emit('update:settingDialog', false)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.inputInfo{width: 50px;}

.attentInfo {
  p {
    padding: 3px 0;
  }
  .el-input--medium {
    .el-input__inner {
      height: 24px;
      line-height: 24px;
    }
  }
}
.titmInfo {
  .el-date-editor--timerange.el-input__inner {
    width: 280px;
  }
  .el-date-editor .el-range-separator {
    padding: 0 15px 0 0;
  }
}
.ruleInfo {
  .el-input--medium .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
.tipInfo {
  p {
    padding: 5px 0;
  }
}
.titInfo {
  border-bottom: 1px solid #dcdfe6;
  height: 30px;
  line-height: 30px;
  padding: 0 0 15px;
}
.attentInfo {
  padding: 30px 15px 15px 80px;
}
.ruleInfo {
  .pad {
    padding-left: 80px;
  }
}
</style>
